﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RLRT.Common;
using RLRT.Models;
using Plusii.iiWeb.Framework;

namespace RLRT.Web
{
    public partial class ItemEdit : LocalDialog
    {
        #region 属性

        /// <summary>
        /// 当前ID
        /// </summary>
        private string CurrentID
        {
            get
            {
                object tempObject = ViewState["CurrentID"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentID"] = value;
            }
        }

        #endregion

        protected override string QueryStringValidationPattern
        {
            get
            {
                return string.Empty; //关闭非法参数过滤
            }
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                string id = Request.QueryString["ID"];
                if (id != null && id.Trim().Length != 0)
                {
                    this.CurrentID = id.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素

            this.AssignDialogCallback(CancelButton, "'Close'", "window");

            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容

            using (iiConfig configProvider = new iiConfig())
            {
                var imtypTable = configProvider.GetList("ITY");
                LocalGlobal.BindListItems(imtyp, imtypTable.DefaultView, "Name", "Key", true);

                var impumTable = configProvider.GetList("UIT");
                LocalGlobal.BindListItems(impum, impumTable.DefaultView, "Name", "Key", true);
            }

            #endregion

            if (this.CurrentID.Length == 0)
            {
                //新增
            }
            else
            {
                //编辑
                this.BindItem();
            }
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定项目
        /// </summary>
        private void BindItem()
        {
            using (RTProvider rtProvider = new RTProvider())
            {
                DataRow row = rtProvider.GetItemDetail(this.CurrentID);
                if (row != null)
                {
                    improd.Text = row["improd"].ToString();
                    imdes.Text = row["imdes"].ToString();
                    imname.Text = row["imname"].ToString();
                    imfac.SelectedValue = row["imfac"].ToString().Trim();
                    immode.Text = row["immode"].ToString();
                    imvnam.Text = row["imvnam"].ToString();
                    imium.Text = row["imium"].ToString();
                    imtyp.SelectedValue = row["imtyp"].ToString().Trim();
                    imukg.Text = row["imukg"].ToString();
                    imusiz.Text = row["imusiz"].ToString();
                    immax.Text = row["immax"].ToString();
                    immin.Text = row["immin"].ToString();
                    imsct.Text = row["imsct"].ToString();
                    imnct.Text = row["imnct"].ToString();
                    impum.SelectedValue = row["impum"].ToString().Trim();
                    imcvr.Text = row["imcvr"].ToString();
                    imbpc.Text = row["imbpc"].ToString().Trim();
                    imkpc.Text = row["imkpc"].ToString();
                    imltm.Text = row["imltm"].ToString();
                    imabc.SelectedValue = row["imabc"].ToString().Trim();
                }

            }
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            using (RTProvider rtProvider = new RTProvider())
            {
                string cvr = imcvr.Text.Trim().Length == 0 ? "0" : imcvr.Text.Trim();
                string ukg = imukg.Text.Trim().Length == 0 ? "0" : imukg.Text.Trim();
                string usiz = imusiz.Text.Trim().Length == 0 ? "0" : imusiz.Text.Trim();
                string max = immax.Text.Trim().Length == 0 ? "0" : immax.Text.Trim();
                string min = immin.Text.Trim().Length == 0 ? "0" : immin.Text.Trim();
                string sct = imsct.Text.Trim().Length == 0 ? "0" : imsct.Text.Trim();
                string nct = imnct.Text.Trim().Length == 0 ? "0" : imnct.Text.Trim();
                string ltm = imltm.Text.Trim().Length == 0 ? "0" : imltm.Text.Trim();

                if (this.CurrentID.Length == 0)
                {
                    //新增
                    try
                    {
                        DataRow row = rtProvider.GetItemDetail(improd.Text.Trim());
                        if (row != null)
                        {
                            this.ShowErrorMessage("料号重复!请重新输入");
                            return;
                        }

                        rtProvider.InsertItemInfo(improd.Text.Trim(), imdes.Text.Trim(), imname.Text.Trim(), imfac.SelectedValue.Trim(), immode.Text.Trim(), imvnam.Text.Trim(), imium.Text.Trim(), impum.SelectedValue.Trim(), cvr, imtyp.SelectedValue.Trim(), ukg, usiz, max, min, sct, nct, nct, imabc.SelectedValue.Trim(), imbpc.Text.Trim(), imkpc.Text.Trim(), ltm, "0", DateTime.Now, DateTime.Now, LocalGlobal.CurrentUserID);
                    }
                    catch (Exception error)
                    {
                        this.ShowErrorMessage(this.GetGlobalResourceString("CreateErrorMessage") + error.Message);
                        return;
                    }
                }
                else
                {
                    //编辑
                    try
                    {
                        rtProvider.UpdateItemInfo(this.CurrentID, imdes.Text.Trim(), imname.Text.Trim(), imfac.SelectedValue.Trim(), immode.Text.Trim(), imvnam.Text.Trim(), imium.Text.Trim(), impum.SelectedValue.Trim(), cvr, imtyp.SelectedValue.Trim(), ukg, usiz, max, min, sct, nct, nct, imabc.SelectedValue.Trim(), imbpc.Text.Trim(), imkpc.Text.Trim(), ltm, "0", DateTime.Now, DateTime.Now, LocalGlobal.CurrentUserID);
                    }
                    catch (Exception error)
                    {
                        this.ShowErrorMessage(this.GetGlobalResourceString("UpdateErrorMessage") + error.Message);
                        return;
                    }
                }
            }
            //回调
            this.DialogCallback("'CloseRefresh'", "window");
        }

        #endregion
    }
}